package com.nhjf.dao.system;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.nhjf.dao.BaseDao;
import com.nhjf.model.system.Role;
import com.nhjf.pojo.qo.system.RoleQo;

@Repository
public class RoleDao extends BaseDao<Role,RoleQo>{

	@Override
	protected Criteria buildCriteria(Criteria criteria, RoleQo qo) {
		if(qo != null){
			
			criteria.setFetchMode("resourceList", FetchMode.SELECT);
			
			if(qo.getRoleId() != null){
				criteria.add(Restrictions.eq("id", qo.getRoleId()));
			}
			
			if(StringUtils.isNotBlank(qo.getName())){
				criteria.add(Restrictions.like("name", qo.getName(),MatchMode.ANYWHERE));
			}
			
			if(StringUtils.isNotBlank(qo.getNumber())){
				criteria.add(qo.getNumberLike()?Restrictions.like("number", qo.getNumber(),MatchMode.ANYWHERE)
						:Restrictions.eq("number", qo.getNumber()));
			}
			
//			criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		}
		return criteria;
	}

	@Override
	protected Class<Role> getEntityClass() {
		return Role.class;
	}

}
